#include "stdafx.h"
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include <cstring>
#include "detail/bmock_out_str_arg_val.hpp"

namespace bmock {
	out_str_arg_val::out_str_arg_val(const char *const n,char *const vp)
		:out_ptr_arg_val<char>(n,vp)
	{}

	out_str_arg_val::~out_str_arg_val() {
	}

	bool out_str_arg_val::update_out_value(const arg_checker &,const std::string &v) const {
		if(ignore(v)) return false;
		try {				
			strcpy(val_ptr_, v.c_str()); 
		} catch(...) {
			return true;
		}
		return false;
	}


	void out_str_arg_val::update(const std::string &v,const control_tools_ptr& t,int index) const {
		try {
			strcpy(val_ptr_,v.c_str()); 
		}
		catch(...) {
			bmock::location loc_;
			std::string Index=boost::lexical_cast<std::string>(index);
			t->error(message(ARG_UPDATE,Index),loc_,true,true);
		}

	}

	std::string out_str_arg_val::get_out_value() const {
		return __get_value(val_ptr_);
	}

	bool out_str_arg_val::null_supplied() const {
		return (NULL==val_ptr_);
	}

	typedef boost::shared_ptr<char> str_pool_ptr;
	static std::vector<str_pool_ptr> str_pool;

	const char *__save_out_str_ptr(const std::string &v) {
		char *p = new char[v.length()+1];
		strcpy(p,v.c_str());
		str_pool.push_back(str_pool_ptr(p));
		return p;
	}
}